##############################
#   调用函数   
#   by Jerry Yang      
#   UUZ0226            
#############################
import pygame
import sys
import random
import time
import string
from skill import Skill
from enemy_attack import EnemyAttack
from enemy import Enemy
from gametext import GameText
import enemy_function as ef
import hero_function as hf
from button import Button


def check_events(ai_settings, screen, stats,hero,skills,cb):
    """响应按键事件"""
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event,ai_settings,stats, screen, hero,skills)
        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x, mouse_y = pygame.mouse.get_pos()
            check_remake_button(stats, cb,mouse_x, mouse_y)
        
def check_keydown_events(event,ai_settings,stats,
                        screen, hero,skills):
    """响应按键"""
    names = list(stats.Skill.keys())
    if event.key == pygame.K_1:
        # 创建一颗技能，并将其加入到编组skills中
        fire_attack(ai_settings, screen,stats, hero, skills)
    elif event.key == pygame.K_2:
        stats.game_pause = False
        stats.skill_flag = True
    elif event.key == pygame.K_q:
        sys.exit()
    elif event.key == pygame.K_i:
        fire_skill(ai_settings, screen,stats, hero, skills)
    elif event.key == pygame.K_j and len(names) >= 2:
        fire_skill(ai_settings, screen,stats, hero, skills)
    elif event.key == pygame.K_k and len(names) >= 3:
        fire_skill(ai_settings, screen,stats, hero, skills)
    elif event.key == pygame.K_l and len(names) == 4:
        fire_skill(ai_settings, screen,stats, hero, skills)

def check_remake_button(stats, cb,mouse_x, mouse_y):
    '''检查是否重随'''
    button_clicked1 = cb.rect1.collidepoint(mouse_x, mouse_y)
    button_clicked2 = cb.rect2.collidepoint(mouse_x, mouse_y)
    if button_clicked1 and not stats.game_active:
        stats.restart_flag = True
        stats.game_active = False
    elif button_clicked2 and not stats.game_active:
        stats.restart_flag = False
        stats.game_active = True


def fire_attack(ai_settings, screen,stats,
        hero, skills):
    '''如果还没有到达限制，就发射'''
    stats.game_pause = False
    if not stats.hurt_flag and stats.game_active and not stats.skill_choose_flag:
        if len(skills) < ai_settings.skills_allowed:
                new_skill = Skill(ai_settings, screen, hero,'Attack')
                skills.add(new_skill)
                stats.skill_flag = False

def fire_skill(ai_settings, screen,stats,
        hero, skills):
    '''如果还没有到达限制，就发射'''
    stats.game_pause = False
    if not stats.hurt_flag and stats.game_active and stats.skill_choose_flag:
        if len(skills) < ai_settings.skills_allowed:
                new_skill = Skill(ai_settings, screen, hero,'S1')
                skills.add(new_skill)
                stats.skill_flag = False

def enemy_fire(ai_settings, screen,stats,
            enemyattacks):
    enemyattacks = stats.enemyattacks
    if stats.hurt_flag:
        new_enemyattack = EnemyAttack(ai_settings, screen)
        enemyattacks.add(new_enemyattack)
        stats.enemyattacks = enemyattacks
        stats.skill_flag = False
        stats.hurt_flag = False

def choose_skill_text(ai_settings, screen,stats,skills):
    skill_inf = stats.Skill
    stats.skill_choose_flag = True
    names = list(skill_inf.keys())
    M_A = list(skill_inf.values())
    for i in range(0,len(names)):
        skill_name = names[i]
        skill_value = list(M_A[i].values())
        skill_magic = skill_value[0]
        skill_attack = skill_value[1]
        skill_letter = string.ascii_letters[34+i]
        text = [skill_letter+'.'+skill_name+':','消耗MP：'+str(skill_magic),'伤害'+str(skill_attack)]    
        for  j in range(0, len(text)):
            m = GameText(ai_settings, screen,text[j],25)
            m.welcome_rect.centerx = m.screen_rect.centerx+(m.size+30)*3*(i-2)+60
            m.welcome_rect.centery = m.screen_rect.centery+(m.size+10)*j
            m.show_welcome()



def update_welcome_screen(ai_settings, screen):
    """开始屏幕"""
    # 每次循环时都重绘屏幕
    screen.fill(ai_settings.bg_color)
    text = ['无限冒险v2.0','作者：子迁']
    #欢迎字符
    for  i in range(0, len(text)):
        m = GameText(ai_settings, screen,text[i],40)
        m.welcome_rect.centery = m.screen_rect.centery+50*(i-1)
        m.show_welcome()
    # 让最近绘制的屏幕可见
    pygame.display.flip()
    time.sleep(2)

def update_information_screen(ai_settings, screen,stats,cb):
    """开始信息屏幕"""
    # 每次循环时都重绘屏幕
    screen.fill(ai_settings.bg_color)
    text = ['欢迎来到无限冒险，勇士','以下是你的属性：']
    #欢迎字符
    for  i in range(0, len(text)):
        m = GameText(ai_settings, screen,text[i],35)
        m.welcome_rect.top = m.screen_rect.top+(m.size+10)*i
        m.show_welcome()
    hf.show_information(ai_settings,screen,stats,'center')
    #创建按钮
    cb.draw_button1()
    cb.draw_button2()
    # 让最近绘制的屏幕可见
    pygame.display.flip()


def update_screen(ai_settings, screen,stats,
                hero,skills,enemyattacks,enemys):
    """更新屏幕上的图像，并切换到新屏幕"""
    enemyattacks = stats.enemyattacks
    # 每次循环时都重绘屏幕
    screen.fill(ai_settings.bg_color)
    # 在后面重绘
    for skill in skills.sprites():
        skill.draw_skill()
    #敌人攻击
    for enemyattack in enemyattacks.sprites():
        enemyattack.draw_enemyattack()
    
    hero.blitme()
    enemys.draw(screen)
    #显示信息
    hf.show_information(ai_settings,screen,stats,'left')
    ef.show_enemy(ai_settings,screen,stats.enemy_inf)
    if stats.game_pause:
        dosomething(ai_settings, screen)
    if stats.skill_flag:
        choose_skill_text(ai_settings, screen,stats,skills)
    # 让最近绘制的屏幕可见
    pygame.display.flip()

def update_skills(ai_settings,skills,stats,screen,
                enemyattacks,enemys):
    """更新位置，并删除已消失"""
    skills.update()
    # 删除已消失
    for skill in skills.copy():
        if skill.rect.bottom <= 0:
            skills.remove(skill)
    collisions = pygame.sprite.groupcollide(
        skills, enemys, True, False)
    if collisions:
        stats.hurt_flag = True
        enemy_fire(ai_settings, screen,stats,
                                enemyattacks)
        enemyattacks = stats.enemyattacks
    enemyattacks.update()
    for enemyattack in enemyattacks.copy():
        if enemyattack.rect.top >= ai_settings.screen_height-200:
            enemyattacks.remove(enemyattack)


def create_fleet(ai_settings,stats,
                screen, enemys):
    """创建敌人"""
    if len(enemys) == 0:
        enemy_inf = stats.enemy_inf
        name = ef.EnemySpecies()
        enemy = Enemy(ai_settings, screen,name)
        enemys.add(enemy)
        #创建名字
        name_all = ef.EnemyName()+ef.EnemyName()+name
        enemy_inf['EnemyName'] = name_all
        enemy_inf = ef.EnemyInf(stats.inf,enemy_inf)
        stats.enemy_inf = enemy_inf
        stats.game_pause = True

def dosomething(ai_settings, screen):
    """做些什么"""
    text = ['做些什么：','1.攻击\n2.技能\n3.逃跑\n4.发呆']
    for  i in range(0, len(text)):
        m = GameText(ai_settings, screen,text[i],30)
        m.welcome_rect.centerx = m.screen_rect.centerx
        m.welcome_rect.centery = m.screen_rect.centery+(m.size+10)*i
        m.show_welcome()


